const express = require('express');
const router = express.Router();
const pool = require('../pool');

//查询所有车位
router.get('/query_lots/:qid', (req, res, next) => {
  pool.query(
    `select id,isEmpty,isEnable,predetermined,arrive_time from ParkLotsInfo where id like ?`,
    ['%' + req.params.qid + '%'],
    (err, result) => {
      if (err) return next(err);
      if (result.length == 0) {
        res.send({
          code: 400,
          msg: '未找到相关信息',
        });
      } else {
        res.send({
          code: 200,
          msg: 'success',
          data: result,
        });
      }
    }
  );
});

//查询指定车位
router.get('/query_lot/:qid', (req, res, next) => {
  pool.query(
    `select * from ParkLotsInfo where id like ?`,
    [`%${req.params.qid}%`],
    (err, result) => {
      if (err) return next(err);
      if (result.length == 0) {
        res.send({
          code: 400,
          msg: '未找到相关信息',
        });
      } else {
        res.send({
          code: 200,
          msg: 'success',
          data: result,
        });
      }
    }
  );
});

//修改车位信息
router.post('/updateSeat', (req, res, next) => {
  let { isEmpty, isEnable, predetermined, arrive_time, inFo, id } = req.body;
  pool.query(
    `update parklotsinfo set isEmpty=?,isEnable=?,predetermined=?,arrive_time=?,inFo=? where id=?;`,
    [isEmpty, isEnable, predetermined, arrive_time, inFo, id],
    (err, result) => {
      if (err) next(err);
      console.log(result);
      if (result.affectedRows != 0) {
        res.send({
          code: 200,
          msg: '修改成功',
        });
      } else {
        res.send({
          code: 401,
          msg: '修改失败',
        });
      }
    }
  );
});

// 添加车位
router.post('/addSeat', (req, res, next) => {
  let { isEmpty, isEnable, predetermined, arrive_time, inFo, id } = req.body;
  if (arrive_time) arrive_time = null;
  pool.query(
    `INSERT INTO ParkLotsInfo VALUES(?,?,?,?,?,?);`,
    [id, isEmpty * 1, isEnable * 1, predetermined * 1, arrive_time, inFo],
    (err, result) => {
      if (err) next(err);
      if (result.affectedRows != 0) {
        res.send({
          code: 200,
          msg: '添加成功',
        });
      } else {
        res.send({
          code: 401,
          msg: '添加失败',
        });
      }
    }
  );
});

//查询可预订车位
router.get('/predetermine', (req, res, next) => {
  const size = req.query.size * 1;
  const start = (req.query.nowPage - 1) * size;
  const sql1 = `SELECT id FROM parklotsinfo WHERE isEmpty=0 and isEnable=0 and predetermined=0 limit ${start},${size};`;
  const sql2 = `SELECT ceil(count(id) / ${size}) as pageCount FROM parklotsinfo WHERE isEmpty=0 and isEnable=0 and predetermined=0;`;
  const sql3 = `SELECT ${req.query.nowPage} as nowPage;`;
  const sql = sql1 + sql2 + sql3;
  pool.query(sql, (err, result) => {
    if (err) next(err);
    if (result.length == 0) {
      res.send({
        code: 400,
        msg: '未找到相关信息',
      });
    } else {
      res.send({
        code: 200,
        msg: 'success',
        data: {
          data: result[0],
          pageCount: result[1][0].pageCount,
          nowPage: result[2][0].nowPage,
        },
      });
    }
  });
});

//修改可预订车位
router.put('/alterPred', (req, res, next) => {
  pool.query(
    `update parklotsinfo set predetermined=1,arrive_time=? where id=?;
    update login set booklotsnum = ?,balance=? where username = ?`,
    [
      req.body.arrive_time,
      req.body.id,
      req.body.id,
      req.body.balance,
      req.body.username,
    ],
    (err, result) => {
      if (err) next(err);
      if (result.changeRows != 0) {
        res.send({
          code: 200,
          msg: '预定成功',
        });
      } else {
        res.send({
          code: 401,
          msg: '预定失败',
        });
      }
    }
  );
});

//用户预定车位号和余额和预定价格
router.post('/Prednum', (req, res, next) => {
  pool.query(
    `select booklotsnum,balance from login where username=?;
    select price4 from setprice where id=1;`,
    [req.body.username],
    (err, result) => {
      if (err) next(err);

      if (result.length == 0) {
        res.send({
          code: 400,
          msg: '未找到相关信息',
        });
      } else {
        res.send({
          code: 200,
          msg: '查询成功',
          data: {
            booklotsnum: result[0][0].booklotsnum,
            balance: result[0][0].balance,
            price: result[1][0].price4,
          },
        });
      }
    }
  );
});

module.exports = router;
